package com.community.zhihuishequ.view.car;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.community.zhihuishequ.R;
import com.community.zhihuishequ.view.base.BaseActivity;
import com.community.zhihuishequ.widget.CalendarDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.community.zhihuishequ.widget.CalendarDialog.*;

/**
 * 车辆进出
 */
public class CarManagerListActivity extends BaseActivity {

    @BindView(R.id.appBarLayout)
    AppBarLayout appBarLayout;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.rv_car_list)
    RecyclerView rvCarList;
    @BindView(R.id.toolBar)
    Toolbar toolBar;
    @BindView(R.id.rl_search)
    RelativeLayout rlSearch;
    private ContentAdapter adapter;
    private List<CarData> dataList;
    private List<CarData> contentList;
    private int templateType = 1;
    private String templateStartTime;
    private String templateEndTime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager_car_list);
        unbinder = ButterKnife.bind(this);
        initActionBar(toolBar, true);
        toolBar.setNavigationIcon(R.drawable.ic_back);
        initData();
        initList();
    }

    private void initData() {
        dataList = new ArrayList<>();
        String plateNo = "粤TG12";
        for (int i = 0; i < 20; i++) {
            CarData carData = new CarData();
            carData.setPlateNo(plateNo + String.valueOf(i));
            carData.setStaus(i % 2 == 0 ? 2 : 3);
            carData.setTransitTime(TimeUtils.getNowString());
            dataList.add(carData);
        }
    }

    private void search(int type, String start, String endTime, String contentString) {
        if (contentList == null) {
            contentList = new ArrayList<>();
            contentList.addAll(dataList);
        }
        List<CarData> templateList = new ArrayList<>();
        for (CarData carData : contentList) {
            if (type != DIALOG_ALL) {
                if (carData.getStaus() != type) {
                    continue;
                }
            }
//            if (!StringUtils.isEmpty(start)) {
//                if (TimeUtils.string2Date(carData.getTransitTime()).getTime() < TimeUtils.string2Date(start).getTime()) {
//                    continue;
//                }
//            }
//            if (!StringUtils.isEmpty(endTime)) {
//                if (TimeUtils.string2Date(carData.getTransitTime()).getTime() > TimeUtils.string2Date(endTime).getTime()) {
//                    continue;
//                }
//            }
            if (!StringUtils.isEmpty(contentString)) {
                if (!carData.getPlateNo().contains(contentString)) {
                    continue;
                }
            }
            templateList.add(carData);
        }
        dataList.clear();
        adapter.notifyDataSetChanged();
        dataList.addAll(templateList);
        adapter.notifyDataSetChanged();
    }

    private void initList() {
        adapter = new ContentAdapter(R.layout.item_car_manager, dataList);
        rvCarList.setLayoutManager(new LinearLayoutManager(this));
        rvCarList.setAdapter(adapter);
    }

    @OnClick(R.id.rl_search)
    public void onViewClicked() {
        search(templateType, templateStartTime, templateEndTime, etSearch.getText().toString());
    }

    private class ContentAdapter extends BaseQuickAdapter<CarData, BaseViewHolder> {

        public ContentAdapter(int layoutResId, @Nullable List<CarData> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, CarData item) {
            helper.setText(R.id.tv_carPlate, item.getPlateNo());
            helper.setText(R.id.tv_transit_time, "通过时间：" + item.getTransitTime());
            if (item.getStaus() == 2) {
                helper.setText(R.id.tv_status, "进场");
                helper.setBackgroundRes(R.id.tv_status, R.drawable.bg_yellow);
            } else if (item.getStaus() == 3) {
                helper.setText(R.id.tv_status, "离场");
                helper.setBackgroundRes(R.id.tv_status, R.drawable.bg_gray);
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        if (item.getItemId() == R.id.item_calendar) {
            new CalendarDialog.Builder(this)
                    .setRadioVisible(true)
                    .setCallback(new CalendarDialog.Callback() {
                        @Override
                        public void onConfirmContent(int type, String start, String endTime) {
                            templateType = type;
                            templateStartTime = start;
                            templateEndTime = endTime;
                            search(type, start, endTime, etSearch.getText().toString());
                        }
                    })
                    .show();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_car, menu);
        return true;
    }
}
